package com.erplan.swt.app.gx;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class GxSplashScreen {

	public static void showSplashScreen() {
		final int progressLength = 100;
		final Image image = new Image(Gx.display, 300, 300);
		//final Image image = new Image(display, Gx.class.getResourceAsStream("images/splash.gif"));
	    final Shell splash = new Shell(SWT.ON_TOP);
	    final ProgressBar bar = new ProgressBar(splash, SWT.NONE);


	    bar.setMaximum(progressLength);
	    Label splashLabel = new Label(splash, SWT.NONE);
	    splashLabel.setImage(image);
	    FormLayout formLayout = new FormLayout();
	    splash.setLayout(formLayout);
	    FormData splashLabelData = new FormData();
	    splashLabelData.right = new FormAttachment(100, 0);
	    splashLabelData.bottom = new FormAttachment(100, 0);
	    splashLabel.setLayoutData(splashLabelData);
	    FormData progressData = new FormData();
	    progressData.left = new FormAttachment(0, 5);
	    progressData.right = new FormAttachment(100, -5);
	    progressData.bottom = new FormAttachment(100, -5);
	    bar.setLayoutData(progressData);
	    splash.pack();
	    Rectangle splashRect = splash.getBounds();
	    Rectangle displayRect = Gx.display.getBounds();
	    int splashX = (displayRect.width - splashRect.width) / 2;
	    int splashY = (displayRect.height - splashRect.height) / 2;
	    splash.setLocation(splashX, splashY);

	    splash.open();
	    for(int i=1 ; i<progressLength ; i = i + i){
		    bar.setSelection(i);
		    try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
	    }
		splash.close();
	}

}
